package by.airlanterns.web;

import by.airlanterns.dto.Lantern;
import by.airlanterns.service.LanternService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by IntelliJ IDEA.
 * User: Sergey
 * Date: 30.01.13
 * Time: 17:27
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping(value = "lantern")
public class LanternController {

    @Autowired
    private LanternService lanternService;

    @RequestMapping(value = "get_lantern_by_id", method = RequestMethod.GET)
    private void getLanternById(HttpServletRequest request, HttpServletResponse response) {
        int lId = Integer.parseInt(request.getParameter("id"));
        Lantern lantern = lanternService.getLanternById(lId);
        try {
            PrintWriter pw = response.getWriter();
            pw.write(lantern.getDescription());
            response.setStatus(HttpServletResponse.SC_OK);
        } catch (IOException e) {
            response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
            e.printStackTrace();
        }
    }
}
